﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace Twi.COMMON.WPF
{
    public class UComboBox : ComboBox
    {
        public string SelectedValuePath1
        {
            get { return GetValue(SelectedValuePath1Property).ToString(); }
            set { SetValue(SelectedValuePath1Property, value); }
        }
        public string SelectedValuePath2
        {
            get { return GetValue(SelectedValuePath2Property).ToString(); }
            set { SetValue(SelectedValuePath2Property, value); }
        }
        public string SelectedValuePath3
        {
            get { return GetValue(SelectedValuePath3Property).ToString(); }
            set { SetValue(SelectedValuePath3Property, value); }
        }
        public static readonly DependencyProperty SelectedValuePath1Property = DependencyProperty.Register("SelectedValuePath1", typeof(string), typeof(UComboBox), new PropertyMetadata("", null));
        public static readonly DependencyProperty SelectedValuePath2Property = DependencyProperty.Register("SelectedValuePath2", typeof(string), typeof(UComboBox), new PropertyMetadata("", null));
        public static readonly DependencyProperty SelectedValuePath3Property = DependencyProperty.Register("SelectedValuePath3", typeof(string), typeof(UComboBox), new PropertyMetadata("", null));

        public bool _stopOnSelectionChanged = false;
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            if (_stopOnSelectionChanged) return;
            if (e.AddedItems.Count == 0)
            {
                SetValue(SelectedValue1Property, "");
            }
            else
            {
                var s1 = this.SelectedValuePath1.Split(new string[] { "Element[", "].Value" }, StringSplitOptions.RemoveEmptyEntries);
                if (s1.Length > 0)
                {
                    string elementName = s1[0];
                    SetValue(SelectedValue1Property, (e.AddedItems[0] as XElement).Element(elementName).Value);
                }
                var s2 = this.SelectedValuePath2.Split(new string[] { "Element[", "].Value" }, StringSplitOptions.RemoveEmptyEntries);
                if (s2.Length > 0)
                {
                    string elementName2 = s2[0];
                    SetValue(SelectedValue2Property, (e.AddedItems[0] as XElement).Element(elementName2).Value);
                }
                var s3 = this.SelectedValuePath3.Split(new string[] { "Element[", "].Value" }, StringSplitOptions.RemoveEmptyEntries);
                if (s3.Length > 0)
                {
                    string elementName3 = s3[0];
                    SetValue(SelectedValue3Property, (e.AddedItems[0] as XElement).Element(elementName3).Value);
                }
            }
        }

        public static readonly DependencyProperty SelectedValue1Property = DependencyProperty.Register("SelectedValue1", typeof(string), typeof(UComboBox)
            , new PropertyMetadata("", new PropertyChangedCallback(SelectedValue1Changed)));
        public string SelectedValue1
        {
            get 
            {
                return GetValue(SelectedValue1Property).ToString();
       
            }
            set { SetValue(SelectedValue1Property, value); }

        }

        private static void SelectedValue1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            UComboBox gcob = sender as UComboBox;
            var s1 = gcob.SelectedValuePath1.Split(new string[] { "Element[", "].Value" }, StringSplitOptions.RemoveEmptyEntries);
            if (s1.Length > 0) 
            {
                for (int i = 0; i < gcob.Items.Count; i++) 
                {
                    if ((gcob.Items[i] as XElement).Element(s1[0]).Value == e.NewValue.ToString()) 
                    {
                        gcob._stopOnSelectionChanged = true;
                        gcob.SelectedIndex = i;
                        gcob._stopOnSelectionChanged = false;
                        return;
                    }
                }
            }
        }

        public static readonly DependencyProperty SelectedValue2Property = DependencyProperty.Register("SelectedValue2", typeof(string), typeof(UComboBox));
        public string SelectedValue2
        {
            get { return GetValue(SelectedValue2Property).ToString(); }
            set { SetValue(SelectedValue2Property, value); }
        }
        public static readonly DependencyProperty SelectedValue3Property = DependencyProperty.Register("SelectedValue3", typeof(string), typeof(UComboBox));
        public string SelectedValue3
        {
            get { return GetValue(SelectedValue3Property).ToString(); }
            set { SetValue(SelectedValue3Property, value); }
        }
        public static readonly DependencyProperty SelectedValue4Property = DependencyProperty.Register("SelectedValue4", typeof(string), typeof(UComboBox));
        public string SelectedValue4
        {
            get { return GetValue(SelectedValue4Property).ToString(); }
            set { SetValue(SelectedValue4Property, value); }
        }
    }
}
